﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web.Profile;

namespace Microsoft.WVB.Server
{
    [ServiceBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ProfileService : IProfileService
    {
        #region IProfileService Members

        [OperationBehavior]
        public CreateProfileResponse CreateProfile(CreateProfileRequest request)
        {
            bool succeed = false;

            if (request.Profile == null)
            {
                throw ExceptionFaultBuilder.GetFault("2001", "ArgumentNullException", "CreateProfile");
            }

            try
            {
                request.Profile.Level = ProfileLevel.Registered;
                ProfileBase profile = ProfileBase.Create(request.Profile.Username, true);
                profile.Initialize(request.Profile.Username, true);

                EntityConvert.PopulateProfileInfo(request.Profile, ref profile);
                profile.Save();

                succeed = true;
            }
            catch (System.Configuration.Provider.ProviderException)
            {
                throw ExceptionFaultBuilder.GetFault("2002", "ProviderException", "CreateProfile");
            }
            catch (System.Configuration.ConfigurationErrorsException)
            {
                throw ExceptionFaultBuilder.GetFault("2003", "ConfigurationErrorsException", "CreateProfile");
            }
            catch (System.Exception)
            {
                throw ExceptionFaultBuilder.GetFault("2004", "UnknownException", "CreateProfile");
            }

            return new CreateProfileResponse(succeed, request.Profile);
        }

        [OperationBehavior]
        public UpdateProfileResponse UpdateProfile(UpdateProfileRequest request)
        {
            bool succeed = false;

            if (request.Profile == null)
            {
                throw ExceptionFaultBuilder.GetFault("2001", "ArgumentNullException", "CreateProfile");
            }

            try
            {
                ProfileBase profile = ProfileBase.Create(request.Profile.Username, true);
                profile.Initialize(request.Profile.Username, true);

                EntityConvert.PopulateProfileInfo(request.Profile, ref profile);
                profile.Save();

                succeed = true;
            }
            catch (System.Configuration.Provider.ProviderException)
            {
                throw ExceptionFaultBuilder.GetFault("2002", "ProviderException", "UpdateProfile");
            }
            catch (System.Configuration.ConfigurationErrorsException)
            {
                throw ExceptionFaultBuilder.GetFault("2003", "ConfigurationErrorsException", "UpdateProfile");
            }
            catch (System.Exception)
            {
                throw ExceptionFaultBuilder.GetFault("2004", "UnknownException", "UpdateProfile");
            }

            return new UpdateProfileResponse(succeed);
        }

        [OperationBehavior]
        public DeleteProfileResponse DeleteProfile(DeleteProfileRequest request)
        {
            bool succeed = false;

            if (String.IsNullOrEmpty(request.UserName))
            {
                throw ExceptionFaultBuilder.GetFault("2001", "ArgumentNullException", "DeleteProfile");
            }

            try
            {
                succeed = ProfileManager.DeleteProfile(request.UserName);
            }

            catch (Exception)
            {
                throw ExceptionFaultBuilder.GetFault("2005", "UnknownException", "DeleteProfile");
            }

            return new DeleteProfileResponse(succeed);
        }

        [OperationBehavior]
        public GetProfileResponse GetProfile(GetProfileRequest request)
        {
            GetProfileResponse response = null;

            if (String.IsNullOrEmpty(request.Username))
            {
                throw ExceptionFaultBuilder.GetFault("2001", "ArgumentNullException", "DeleteProfile");
            }

            try
            {
                ProfileBase profile = ProfileBase.Create(request.Username, true);
                profile.Initialize(request.Username, true);

                WVBProfile shadowProfile = EntityConvert.ConvertToWVBProfile(profile);
                response = new GetProfileResponse(shadowProfile);
            }
            catch (System.Configuration.Provider.ProviderException)
            {
                throw ExceptionFaultBuilder.GetFault("2002", "ProviderException", "UpdateProfile");
            }
            catch (System.Configuration.ConfigurationErrorsException)
            {
                throw ExceptionFaultBuilder.GetFault("2003", "ConfigurationErrorsException", "UpdateProfile");
            }
            catch (System.Exception)
            {
                throw ExceptionFaultBuilder.GetFault("2004", "UnknownException", "UpdateProfile");
            }
            return response;
        }

        #endregion
    }

}
